<?php
/**
 * Teacher Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * teacher entry
 *
 * @version $Id: Teacher.php 94 2010-05-02 15:04:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Teacher_Model_TeacherMapper
 * @package teacher
 * @subpackage Model
 */
class Teacher_Model_Teacher extends Teacher_Model_Doctrine_BaseTeacher
{
  //  private $_arrayData ;
    /**
     * Constructor
     *
     * @param array|null $options
     * @return void
     */
//    public function __construct()
//    {
//
//        parent::__construct();
//    }


    public function getTeacherKind()
    {
         $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $arrayData = $ini->get('array_data')->toArray();
        return $arrayData['current_kind_sn'];
    }

    /**
     * @param integer $condition_state
     * @return multitype: array
     */
    public function getTeachers($condition_state=0)
    {

        $q = Doctrine_Query::create()
        ->select('a.current_kind_sn, a.username, a.name, a.sn')
        ->from('Teacher_Model_TeacherView a')
        ->where ('a.condition_state=?',$condition_state)
        ->orderBy('a.current_kind_sn, a.name');

        $res = $q->fetchArray();
        $arr = array();
        $kind_arr = $this->getTeacherKind();
        foreach ($res as $row) {
            $sn = $kind_arr[$row['current_kind_sn']];
            $arr[$sn][] = $row;
        }
        return $arr;
    }

    static public function getSnById($id='')
    {
        if ($id == '')
        $id = Zend_Auth::getInstance()->getIdentity()->username;
        $q = Doctrine_Query::create()
        ->select('sn')
        ->from('Teacher_Model_Teacher')
        ->where('username=?',$id);

        $res = $q->fetchOne();

        return $res['sn'];
    }

    /**
     *  教師任教班級
     * @param integer $id username
     * @return multitype: string boolean
     */
    static  public function getTutorClass($id='')
    {
        $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
        $yearSemester = $view->Semester;
        $year = $yearSemester['year'];
        $semester = $yearSemester['semester'];
        if ($id == '')
        $id = Zend_Auth::getInstance()->getIdentity()->username;
        $q = Doctrine_Query::create()
        ->select('class_num')
        ->from('Semester_Model_SemesterTeacher')
        ->where('year=?', $year)
        ->andWhere('semester=?', $semester)
        ->andWhere('teacher_id=?', $id);
        $res = $q->fetchOne();
//        echo $q->getSqlQuery();
//        echo $year.'--'.$semester.'--'.$id;
        if ($res)
        return $res->class_num;
        else
        return false;
    }
}
